﻿/*
Copyright (c) 2010, hiSoft
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.
    * Neither the name of the hiSoft nor the
      names of its contributors may be used to endorse or promote products
      derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE HISOFT AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL HISOFT BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Orchard.Test.Library;
using Selenium;

namespace Orchard.Test.Modules.Widgets.Auto
{
    [TestClass]
    public class AddLayerWithInvalidRules
    {
        private TestLibrary TestLibrary;
        private ISelenium selenium;

        [TestInitialize]
        public void SetupTest()
        {
            TestLibrary = TestLibrary.SetupTest(this.ToString());
            selenium = TestLibrary.Selenium;
        }

        [TestCleanup]
        public void TeardownTest()
        {
            TestLibrary.ShutDown();
        }

        [TestMethod]
        public void Widget_AddLayerWithInvalidRules()
        {
            string errorMessage = "";
            string validPartMessage = "The rule is not valid: ";
            try
            {
                TestLibrary.UserHelper.LogOnAsAdmin();
                TestLibrary.WidgetHelper.AddWidgetLayer("Layer1", TestLibrary.Consts.AdminUserName, "This is layer01", "url ('~/default*') & authenticated");
                Assert.IsTrue(selenium.IsTextPresent(validPartMessage+"Syntax error: Unexpected token in primary expression (Token &amp;#39;Invalid&amp;#39; (Unrecognized character) at position 19)"));
                //Assert.IsTrue(selenium.GetLocation().Contains());
                TestLibrary.WidgetHelper.AddWidgetLayer("Layer2", TestLibrary.Consts.AdminUserName, "This is layer02", "url ('~/default*') && 123=123");
                Assert.IsTrue(selenium.IsTextPresent(validPartMessage + "Syntax error: Unexpected token in primary expression (Token &amp;#39;Equal&amp;#39; at position 25)"));
                //var widget411 = TestLibrary.WidgetHelper.AddHtmlWidget(layer11, WidgetZone.Header, "widget4Layer11", 1, TestLibrary.Consts.AdminUserName, "widget for layer11");
                TestLibrary.WidgetHelper.AddWidgetLayer("Layer3", TestLibrary.Consts.AdminUserName, "This is layer03", "url ('~/default*') && a<=b");
                Assert.IsTrue(selenium.IsTextPresent(validPartMessage + "&#39;null&#39; values can not be compared"));
                TestLibrary.WidgetHelper.AddWidgetLayer("Layer4", TestLibrary.Consts.AdminUserName, "This is layer04", "url ('~/default*') && 'a'>'b'");
                Assert.IsTrue(selenium.IsTextPresent(validPartMessage + "String values can not be compared"));
                TestLibrary.WidgetHelper.AddWidgetLayer("Layer5", TestLibrary.Consts.AdminUserName, "This is layer05", "url ('~/default*') && @>$");
                Assert.IsTrue(selenium.IsTextPresent(validPartMessage + "Syntax error: Unexpected token in primary expression (Token &amp;#39;Invalid&amp;#39; (Unrecognized character) at position 22)"));
                TestLibrary.WidgetHelper.AddWidgetLayer("Layer6", TestLibrary.Consts.AdminUserName, "This is layer06", "url ('~/default*') && '\\'=='\\'");
                Assert.IsTrue(selenium.IsTextPresent(validPartMessage + "Syntax error: Unexpected token in primary expression (Token &amp;#39;Invalid&amp;#39; (Unrecognized character) at position 28)"));
                TestLibrary.WidgetHelper.AddWidgetLayer("Layer7", TestLibrary.Consts.AdminUserName, "This is layer07", "url ('~/default*') && '16'+'15'=='1615'");
                Assert.IsTrue(selenium.IsTextPresent(validPartMessage + "Value &#39;16&#39; is not convertible to an integer."));
                TestLibrary.WidgetHelper.AddWidgetLayer("Layer8", TestLibrary.Consts.AdminUserName, "This is layer08", "url ('~/default*') && 10==10/0");
                Assert.IsTrue(selenium.IsTextPresent(validPartMessage + "Attempted to divide by zero"));
                TestLibrary.WidgetHelper.AddWidgetLayer("Layer9", TestLibrary.Consts.AdminUserName, "This is layer09", "url ('~/default*') && 10==8.0+2.0");
                Assert.IsTrue(selenium.IsTextPresent(validPartMessage + "Syntax error: Unexpected token in primary expression (Token &amp;#39;Invalid&amp;#39; (Unrecognized character) at position 27)"));
                TestLibrary.WidgetHelper.AddWidgetLayer("Layer10", TestLibrary.Consts.AdminUserName, "This is layer10", "url ('~/default*') && 10<12345678912");
                Assert.IsTrue(selenium.IsTextPresent(validPartMessage + "Value was either too large or too small for an Int32"));
                TestLibrary.WidgetHelper.AddWidgetLayer("Layer11", TestLibrary.Consts.AdminUserName, "This is layer11", "url ('~/default*') && (10>1)/123");
                Assert.IsTrue(selenium.IsTextPresent(validPartMessage + "Value &#39;True&#39; is not convertible to an integer"));

                TestLibrary.WidgetHelper.AddWidgetLayer("Layer12", TestLibrary.Consts.AdminUserName, "This is layer12", "url ('~/default*') && (10>1)>123");
                Assert.IsTrue(selenium.IsTextPresent(validPartMessage + "Boolean values can only be compared to other boolean values"));
                TestLibrary.WidgetHelper.AddWidgetLayer("Layer122", TestLibrary.Consts.AdminUserName, "This is layer122", "url ('~/default*') && (10>1)==123");
                Assert.IsTrue(selenium.IsTextPresent(validPartMessage + "Boolean values can only be compared to other boolean values"));
                TestLibrary.WidgetHelper.AddWidgetLayer("Layer13", TestLibrary.Consts.AdminUserName, "This is layer13", "url ('~/default*') && 3>false");
                Assert.IsTrue(selenium.IsTextPresent(validPartMessage + "Integer values can only be compared to other integer values"));
                TestLibrary.WidgetHelper.AddWidgetLayer("Layer132", TestLibrary.Consts.AdminUserName, "This is layer132", "url ('~/default*') && 3==false");
                Assert.IsTrue(selenium.IsTextPresent(validPartMessage + "Integer values can only be compared to other integer values"));


                TestLibrary.WidgetHelper.AddWidgetLayer("Layer14", TestLibrary.Consts.AdminUserName, "This is layer14", "url ('~/default*') && !null");
                Assert.IsTrue(selenium.IsTextPresent(validPartMessage + "Value &#39;&lt;null&gt;&#39; is not convertible to a boolean"));
                TestLibrary.WidgetHelper.AddWidgetLayer("Layer15", TestLibrary.Consts.AdminUserName, "This is layer15", " url ('~/default*') && ");
                Assert.IsTrue(selenium.IsTextPresent(validPartMessage + "Syntax error: Unexpected token in primary expression (Token &amp;#39;Eof&amp;#39; at position 22)"));
                TestLibrary.WidgetHelper.AddWidgetLayer("Layer16", TestLibrary.Consts.AdminUserName, "This is layer16", " url ('~/default*') && >");
                Assert.IsTrue(selenium.IsTextPresent(validPartMessage + "Index was outside the bounds of the array"));
                TestLibrary.WidgetHelper.AddWidgetLayer("Layer17", TestLibrary.Consts.AdminUserName, "This is layer17", "url ('~/default*') &&  null>123");
                Assert.IsTrue(selenium.IsTextPresent(validPartMessage + "&#39;null&#39; values can not be compared"));

                TestLibrary.WidgetHelper.AddWidgetLayer("Layer18", TestLibrary.Consts.AdminUserName, "This is layer18", "url ('~/default*') &&  ! nil");
                Assert.IsTrue(selenium.IsTextPresent(validPartMessage + "Value &#39;&lt;null&gt;&#39; is not convertible to a boolean"));
                TestLibrary.WidgetHelper.AddWidgetLayer("Layer19", TestLibrary.Consts.AdminUserName, "This is layer19", "url ('~/default*') &&  not null");
                Assert.IsTrue(selenium.IsTextPresent(validPartMessage + "Syntax error: Unexpected token in primary expression (Token &amp;#39;NullLiteral&amp;#39; at position 27)"));
                //('~/default*') && true was invalid expression before,has changed to vaild @2011.08, value consider as true,widget will displayed on everyPage, 
                TestLibrary.WidgetHelper.AddWidgetLayer("Layer20", TestLibrary.Consts.AdminUserName, "This is layer20", "url ('~/default*') &&  1 || 2");
                Assert.IsTrue(selenium.IsTextPresent(validPartMessage + "Expression is not a boolean value"));
                TestLibrary.WidgetHelper.AddWidgetLayer("Layer21", TestLibrary.Consts.AdminUserName, "This is layer21", "ur('~/default*') && true");  
                Assert.IsTrue(selenium.IsTextPresent(validPartMessage + "Expression is not a boolean value"));
                TestLibrary.WidgetHelper.AddWidgetLayer("Layer22", TestLibrary.Consts.AdminUserName, "This is layer22", "url ('~/default*') &&  1/( )");
                Assert.IsTrue(selenium.IsTextPresent(validPartMessage + "Syntax error: Expected token CloseParen"));
                TestLibrary.WidgetHelper.AddWidgetLayer("Layer23", TestLibrary.Consts.AdminUserName, "This is layer23", "true'&'3>2");
                Assert.IsTrue(selenium.IsTextPresent(validPartMessage + "Syntax error: Unexpected token in primary expression (Token &amp;#39;SingleQuotedStringLiteral&amp;#39; (&amp;amp;) at position 4)"));
                               
                
            }
            catch (Exception ex)
            {
                errorMessage += ex.ToString();
            }
            finally
            {
                if (errorMessage.Length > 1)
                    Assert.IsTrue(false, errorMessage);
            }
        }
    }
}
